﻿/********************************************************************
 * *
 * * Copyright (C) 2013-2018 uiskin.cn
 * * 作者： BinGoo QQ：315567586 
 * * 请尊重作者劳动成果，请保留以上作者信息，禁止用于商业活动。
 * *
 * * 创建时间：2014-08-05
 * * 说明：客户端信息类，存储客户端的一些基本信息，可定义修改
 * *
********************************************************************/

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;

namespace NetWorkHelper.IModels
{
    public class IClient
    {
        public IClient()
        {
            ClientStyle = ClientStyle.PcSocket;
            BufferInfo = new BufferInfo();
            ClientInfo = new ClientInfo();
        }

        public IClient(Socket socket)
        {
            WorkSocket = socket;
            if (socket != null)
            {
                Ip = ((IPEndPoint)WorkSocket.RemoteEndPoint).Address.ToString();
                Port = ((IPEndPoint)WorkSocket.RemoteEndPoint).Port;
                IpEndPoint = ((IPEndPoint)WorkSocket.RemoteEndPoint);
            }
            ClientStyle = ClientStyle.PcSocket;
            BufferInfo = new BufferInfo();
            ClientInfo = new ClientInfo();
        }

        private DateTime _heartTime = DateTime.Now;
        /// <summary>
        /// 心跳时间,接收到信息的时间，用于心跳设置
        /// </summary>
        public DateTime HeartTime
        {
            get { return _heartTime; }
            set { _heartTime = DateTime.Now; }
        }

        private bool _isCheckHeart = false;
        /// <summary>
        /// 是否开启心跳检测
        /// </summary>
        public bool IsCheckHeart
        {
            get { return _isCheckHeart; }
            set { _isCheckHeart = value; }
        }
        public IPEndPoint IpEndPoint { get; set; }

        /// <summary>
        /// Socket
        /// </summary>
        public Socket WorkSocket { get; set; }
        /// <summary>
        /// 客户端端口IP
        /// </summary>
        public string Ip { get; set; }
        /// <summary>
        /// 客户端端口
        /// </summary>
        public int Port { get; set; }
        /// <summary>
        /// Socket类型（网页版或者PC版）
        /// </summary>
        public ClientStyle ClientStyle { get; set; }

        /// <summary>
        /// 客户端信息类
        /// </summary>
        public ClientInfo ClientInfo { get; set; }
        /// <summary>
        /// 数据缓存区信息
        /// </summary>
        public BufferInfo BufferInfo { get; set; }
        /// <summary>
        /// 自定义数据
        /// </summary>
        public object CustomData { get; set; }

    }

    public class ClientInfo
    {
        /// <summary>
        /// 心跳检测模式
        /// </summary>
        public HeartCheckType HeartCheckType = HeartCheckType.EncodingString;
        /// <summary>
        /// 心跳包数组数据【如果长度为0为空则不发送心跳包】
        /// </summary>
        public byte[] HeartbeatByte = new byte[0];
        /// <summary>
        /// 心跳包字符串【如果为空则不发送心跳包】
        /// </summary>
        public string Heartbeat = "";
        /// <summary>
        /// 客户端类型
        /// </summary>
        public object ClientType;
        /// <summary>
        /// 客户端信息
        /// </summary>
        public object ClientInfoEx;
        /// <summary>
        /// 客户端订阅参数
        /// </summary>
        public object ClientInfoSubscriptionParam;
    }

    public class BufferInfo
    {
        public BufferInfo(int receivedBufferSize = 1024, int sendBufferSize = 1024)
        {
            ReceivedBuffer = new byte[receivedBufferSize];
            ReceivedBuffer = new byte[sendBufferSize];
        }

        //备份缓冲区
        private byte[] _bufferBackup;
        /// <summary>
        /// 备份缓冲区;动态增大或缩小缓冲区的时候用到；
        /// </summary>
        internal byte[] BufferBackup
        {
            get { return _bufferBackup; }
            set { _bufferBackup = value; }
        }

        /// <summary>
        /// 接收缓冲区
        /// </summary>
        public byte[] ReceivedBuffer;

        /// <summary>
        /// 发送缓冲区
        /// </summary>
        public byte[] SendBuffer;

        /// <summary>
        /// 接收的字符串信息
        /// </summary>
        public string RecevidMsg = "";
    }

    /// <summary>
    /// 心跳检测模式
    /// </summary>
    public enum HeartCheckType
    {
        /// <summary>
        /// 字符串模式
        /// </summary>
        EncodingString,
        /// <summary>
        /// 十六进制字符串
        /// </summary>
        HexString,
        /// <summary>
        /// byte数组模式
        /// </summary>
        Byte
    }
}
